package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("TensorflowService")
@Transactional

public class TensorflowServiceImpl extends CmiEmaService implements TensorflowService {
	public TensorflowServiceImpl() {
	}

	@Transactional
	public Tensorflow saveTensorflow(Tensorflow tensorflow) {

		if (tensorflow.getId() == null) {

			Tensorflow existingTensorflow = tensorflowDAO.findTensorflowByPhysicalPrimaryKey(tensorflow.getName());

			if (isEquals(tensorflow, existingTensorflow)) {
				return existingTensorflow;
			}

			tensorflow = tensorflowDAO.store(tensorflow);
			tensorflowDAO.flush();

		} else {
			Tensorflow existingTensorflow = tensorflowDAO.findTensorflowByPrimaryKey(tensorflow.getId());

			tensorflow = tensorflowDAO.store(tensorflow);
			tensorflowDAO.flush();
		}

		return tensorflow;
	}

	public void deleteTensorflow(Tensorflow tensorflow) {
		tensorflowDAO.remove(tensorflow);
		tensorflowDAO.flush();
	}

	@Transactional
	public void deleteTensorflows(Set<Integer> deleteTensorflowsId) {
		Query query = tensorflowDAO.createNamedQuery("deleteTensorflowsById", -1, -1);
		query.setParameter(1, deleteTensorflowsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Tensorflow> loadTensorflows() {
		return tensorflowDAO.findAllTensorflows();

	}

	@Transactional
	public Set<Tensorflow> loadReferenceTensorflows(Set<Tensorflow> tensorflows) {
		Iterator<Tensorflow> tensorflowsIterator = tensorflows.iterator();
		Set<Tensorflow> referencedTensorflows = new HashSet<>();
		while (tensorflowsIterator.hasNext()) {
			Tensorflow currentTensorflow = tensorflowsIterator.next();

		}
		return referencedTensorflows;
	}

	public Set<Tensorflow> loadTensorflowsByIdSet(Set<Integer> tensorflowIdSet) {

		Iterator<Integer> tensorflowIdIterator = tensorflowIdSet.iterator();
		Set<Tensorflow> tensorflowSet = new HashSet<>();
		while (tensorflowIdIterator.hasNext()) {
			Integer id = tensorflowIdIterator.next();
			Tensorflow currentTensorflow = tensorflowDAO.findTensorflowById(id);
			tensorflowSet.add(currentTensorflow);
		}
		return tensorflowSet;
	}

	@Transactional
	public List<Tensorflow> findAllTensorflows(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Tensorflow>(tensorflowDAO.findAllTensorflows(startResult, maxRows));
	}

	@Transactional
	public Tensorflow findTensorflowByPrimaryKey(Integer id) {
		return tensorflowDAO.findTensorflowByPrimaryKey(id);
	}

	@Transactional
	public Integer countTensorflows() {
		return ((Long) tensorflowDAO.createQuerySingleResult("select count(o) from Tensorflow o").getSingleResult())
				.intValue();
	}

	@Transactional
	public Integer countTensorflows(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Tensorflow.class);
		return ((Long) tensorflowDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countTensorflowsByName(String queryName) {
		Query query = tensorflowDAO.createNamedQuery("countTensorflowsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countTensorflowsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Tensorflow.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadTensorflowObjectReferencedSet(Tensorflow tensorflow) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadTensorflowClassReferencedSet(Tensorflow tensorflow) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();

		return classReferencedSetMap;
	}

	public boolean isEquals(Tensorflow tensorflow, Object obj) {
		if (obj == tensorflow)
			return true;
		if (!(obj instanceof Tensorflow))
			return false;
		Tensorflow comparedTensorflow = (Tensorflow) obj;
		if (!CheckPropertyEqual(tensorflow.getName(), comparedTensorflow.getName())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

